package com.rainbow.dynamic.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.rainbow.dynamic.repository.NacosRouteDefinitionRepository;

@Configuration
@ConditionalOnProperty(prefix = "rainbow.gateway.dynamicRoute", name = "enabled", havingValue = "true")
public class DynamicRouteConfig {
	
	@Autowired
    private ApplicationEventPublisher publisher;
	
    /**
     * Nacos实现方式
     */
    @Configuration
    @ConditionalOnProperty(prefix = "rainbow.gateway.dynamicRoute", name = "dataType", havingValue = "nacos", matchIfMissing = true)
    public class NacosDynRoute {
        
        @Autowired
        private NacosConfigManager nacosConfigManager;
        
        @Value("${rainbow.gateway.dynamicRoute.nacos.routeDataId:scg-routes}")
        private String scgRouteDataId;
        
        @Value("${rainbow.gateway.dynamicRoute.nacos.routeGroup:SCG_GATEWAY}")
        private String scgRouteGroup;

        @Bean
        public NacosRouteDefinitionRepository nacosRouteDefinitionRepository() {
            return new NacosRouteDefinitionRepository(publisher, nacosConfigManager,scgRouteDataId,scgRouteGroup);
        }
    }

}
